﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Frogism.Core;
using Frogism.Domain;
using NUnit.Framework;
using System.Collections.ObjectModel;
namespace Frogism.Tests
{
[TestFixture]
    public class BookStatsTest
    {
        [Test]
        public void BooksCollectionNotNull()
        {
            Collection<IBook> books = new Collection<IBook>();
            BookStats stats = new BookStats(books);
            Assert.IsNotNull(stats.Books);
            Assert.AreEqual(books, stats.Books);
        }
        [Test]
        public void YearReadNoBooks()
        {
            Collection<IBook> books = new Collection<IBook>();
            BookStats stats = new BookStats(books);
            Assert.AreEqual(0,stats.YearRead);
        }
        [Test]
        public void YearReadJustLastYear()
        {
            Collection<IBook> books = new Collection<IBook>();
            Book b = new Book();
            b.Reads.Add(new Read() { Id = 0, Page = 113, ReadDate = DateTime.Now.AddYears(-1) });
            books.Add(b);
            BookStats stats = new BookStats(books);
            Assert.AreEqual(0, stats.YearRead);
        }
        [Test]
        public void YearReadJustThisYear()
        {
            Collection<IBook> books = new Collection<IBook>();
            Book b = new Book();
            b.Reads.Add(new Read() { Id = 0, Page = 113, ReadDate = DateTime.Now });
            books.Add(b);
            BookStats stats = new BookStats(books);
            Assert.AreEqual(113, stats.YearRead);
        }
        [Test]
        public void YearReadThisAndLastYear()
        {
            Collection<IBook> books = new Collection<IBook>();
            Book b = new Book();
            b.Reads.Add(new Read() { Id = 0, Page = 10, ReadDate = DateTime.Now.AddYears(-1) });
            b.Reads.Add(new Read() { Id = 1, Page = 113, ReadDate = DateTime.Now });
            books.Add(b);
            BookStats stats = new BookStats(books);
            Assert.AreEqual(113-10, stats.YearRead);
        }
        [Test]
        public void YearReadJustLastYearWithIndex()
        {
            Collection<IBook> books = new Collection<IBook>();
            Book b = new Book();
            b.IndexLength = 10;
            b.Reads.Add(new Read() { Id = 0, Page = 113, ReadDate = DateTime.Now.AddYears(-1) });
            books.Add(b);
            BookStats stats = new BookStats(books);
            Assert.AreEqual(0, stats.YearRead);
        }
        [Test]
        public void YearReadJustThisYearWithIndex()
        {
            Collection<IBook> books = new Collection<IBook>();
            Book b = new Book();
            b.IndexLength = 10;
            b.Reads.Add(new Read() { Id = 0, Page = 113, ReadDate = DateTime.Now });
            books.Add(b);
            BookStats stats = new BookStats(books);
            Assert.AreEqual(113+10, stats.YearRead);
        }
        [Test]
        public void YearReadThisAndLastYearWithIndex()
        {
            Collection<IBook> books = new Collection<IBook>();
            Book b = new Book();
            b.IndexLength = 10;
            b.Reads.Add(new Read() { Id = 0, Page = 13, ReadDate = DateTime.Now.AddYears(-1) });
            b.Reads.Add(new Read() { Id = 1, Page = 113, ReadDate = DateTime.Now });
            books.Add(b);
            BookStats stats = new BookStats(books);
            Assert.AreEqual(113 - 13, stats.YearRead);
        }
        [Test]
        public void Goal()
        {   
            Collection<IBook> books = new Collection<IBook>();
            BookStats stats = new BookStats(books);
            Assert.AreEqual(DateTime.Now.DayOfYear * 19.2d, stats.Goal);

        }
        [Test]
        public void ReadFirstTimeToday() {
          Collection<IBook> books = new Collection<IBook>();
          IBook book1 = new Book();
          IRead read1 = new Read() { Page = 100, ReadDate = DateTime.Now.CleanDate() };
          book1.Reads.Add(read1);
          books.Add(book1);
          BookStats stats = new BookStats(books);
          Assert.AreEqual(100, stats.ReadToday);
        }
        [Test]
        public void ReadTodayAndYesterday() {
          Collection<IBook> books = new Collection<IBook>();
          IBook book1 = new Book();
          IRead read1 = new Read() { Page = 1, ReadDate = DateTime.Now.AddDays(-1).CleanDate() };
          IRead read2 = new Read() { Page = 100, ReadDate = DateTime.Now.CleanDate() };
          book1.Reads.Add(read1);
          book1.Reads.Add(read2);
          books.Add(book1);
          BookStats stats = new BookStats(books);
          Assert.IsTrue(book1.WasReadToday,"Book1 is not marked as read today.");
          Assert.AreEqual(1,book1.ReadBy(read1.ReadDate),"Ready By function is not returning yesteday's read.");
          Assert.AreEqual(99, stats.ReadToday);
        }
        [Test]
        public void ReadTodayAndLastWeek() {
          Collection<IBook> books = new Collection<IBook>();
          IBook book1 = new Book();
          book1.IndexLength = 33;
          IRead read1 = new Read() { Page = 1, ReadDate = DateTime.Now.AddDays(-7).CleanDate() };
          IRead read2 = new Read() { Page = 100, ReadDate = DateTime.Now.CleanDate() };
          book1.Reads.Add(read1);
          book1.Reads.Add(read2);
          books.Add(book1);
          BookStats stats = new BookStats(books);
          Assert.IsTrue(book1.WasReadToday, "Book1 is not marked as read today.");
          Assert.AreEqual(100 + 33, book1.TotalRead,"Total Read is not correct");
          Assert.AreEqual(1+33, book1.ReadBy(DateTime.Now.AddDays(-1).CleanDate()), "Ready By function is not returning yesterdays pages read.");
          Assert.AreEqual(99, stats.ReadToday);
        }
        [Test]
        public void ReadTodayNotReadToday() {
          Collection<IBook> books = new Collection<IBook>();
          IBook book1 = new Book();
          IRead read1 = new Read() { Page = 1, ReadDate = DateTime.Now.AddDays(-7).CleanDate() };
          book1.Reads.Add(read1);
          books.Add(book1);
          BookStats stats = new BookStats(books);
          Assert.IsFalse(book1.WasReadToday, "Book1 is marked as read today.");
        
        }
    }
}
